package com.example.children.Connection;

import com.example.children.yyz.TechnicalAchievement;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TechAchievementRepository implements JDBCRepository<TechnicalAchievement, String> {

    // 插入一条数据
    @Override
    public void insert(TechnicalAchievement entity) {
        System.out.println("插入一条数据");
        String sql = "INSERT INTO tech_achievement (id,name, description, field) VALUES (?, ?, ?,?)";
        try (Connection connection = DruidConnectionPool.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, entity.getId());
            statement.setString(2, entity.getName());
            statement.setString(3, entity.getDescription());
            statement.setString(4, entity.getField());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 插入多条数据
    @Override
    public void insertBatch(List<TechnicalAchievement> entities) {
        System.out.println("插入多条数据");
        String sql = "INSERT INTO tech_achievement (id,name, description, field) VALUES (?, ?,?,?)";
        try (Connection connection = DruidConnectionPool.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            for (TechnicalAchievement entity : entities) {
                statement.setString(1, entity.getId());
                statement.setString(2, entity.getName());
                statement.setString(3, entity.getDescription());
                statement.setString(4, entity.getField());
                statement.addBatch();
            }
            statement.executeBatch();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据主键更新数据
    @Override
    public void updateById(String id, TechnicalAchievement entity) {
        System.out.println("根据主键更新数据");
        String sql = "UPDATE tech_achievement SET name = ?, description = ?, field = ? WHERE id = ?";
        try (Connection connection = DruidConnectionPool.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, entity.getName());
            statement.setString(2, entity.getDescription());
            statement.setString(3, entity.getField());
            statement.setString(4, id);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateByCondition(String condition, Object[] params, TechnicalAchievement entity) {

    }

    @Override
    public void deleteById(String integer) {

    }

    @Override
    public void deleteByIds(List<String> integers) {

    }

    @Override
    public void deleteByCondition(String condition, Object[] params) {

    }

    // 根据主键查询数据
    @Override
    public TechnicalAchievement findById(String id) {
        String sql = "SELECT * FROM tech_achievement WHERE id = ?";
        try (Connection connection = DruidConnectionPool.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, id);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                TechnicalAchievement achievement = new TechnicalAchievement();
                achievement.setId(resultSet.getString("id"));
                achievement.setName(resultSet.getString("name"));
                achievement.setDescription(resultSet.getString("description"));
                achievement.setField(resultSet.getString("Field"));
                return achievement;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 查询所有数据
    @Override
    public List<TechnicalAchievement> findAll() {
        List<TechnicalAchievement> list = new ArrayList<>();
        String sql = "SELECT * FROM tech_achievement";
        try (Connection connection = DruidConnectionPool.getConnection();
             Statement statement = connection.createStatement()) {
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                TechnicalAchievement achievement = new TechnicalAchievement();
                achievement.setId(resultSet.getString("id"));
                achievement.setName(resultSet.getString("name"));
                achievement.setDescription(resultSet.getString("description"));
                achievement.setField(resultSet.getString("Field"));
                list.add(achievement);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    // 根据条件查询数据
    @Override
    public List<TechnicalAchievement> findByCondition(String condition, Object[] params) {
        List<TechnicalAchievement> list = new ArrayList<>();
        String sql = "SELECT * FROM tech_achievement WHERE " + condition;
        try (Connection connection = DruidConnectionPool.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);
            }
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                TechnicalAchievement achievement = new TechnicalAchievement();
                achievement.setId(resultSet.getString("id"));
                achievement.setName(resultSet.getString("name"));
                achievement.setDescription(resultSet.getString("description"));
                achievement.setField(resultSet.getString("Field"));
                list.add(achievement);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    // 分页查询数据
    @Override
    public List<TechnicalAchievement> findPage(String condition, Object[] params, int pageNum, int pageSize) {
        System.out.println("分页查询");
        List<TechnicalAchievement> list = new ArrayList<>();
        // 构建 SQL 查询语句，包含 LIMIT 和 OFFSET
        String sql = "SELECT * FROM tech_achievement WHERE " + condition +
                " LIMIT ? OFFSET ?";

        try (Connection connection = DruidConnectionPool.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {

            // 设置参数
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);
            }

            // 设置分页参数
            statement.setInt(params.length + 1, pageSize); // 设置 LIMIT
            statement.setInt(params.length + 2, (pageNum - 1) * pageSize); // 设置 OFFSET

            // 执行查询
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                TechnicalAchievement achievement = new TechnicalAchievement();
                achievement.setId(resultSet.getString("id"));
                achievement.setName(resultSet.getString("name"));
                achievement.setDescription(resultSet.getString("description"));
                achievement.setField(resultSet.getString("Field"));
                list.add(achievement);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
